﻿

using OpenCvSharp;

using System;

namespace Xc.DataAccess.CodeReader.Models.Filters
{
    /// <summary>
    /// 过滤器过站
    /// </summary>
    public static class FilterBaseExtension
    {
        /// <summary>
        /// 设置之后运行
        /// </summary>
        /// <typeparam name="TFilteer"></typeparam>
        /// <typeparam name="TCurrent"></typeparam>
        /// <param name="currentFilter"></param>
        /// <param name="callback"></param>
        /// <returns></returns>
        public static TCurrent SetAfter<TFilteer, TCurrent>(this TCurrent currentFilter, Func<TCurrent, FilterBase[], Mat, TFilteer> callback) where TCurrent : FilterBase where TFilteer : FilterBase
        {
            currentFilter.AfterExecute = (a, b, c) =>
            {
                if (b is TCurrent current)
                {
                    return callback?.Invoke(currentFilter, a, c);
                }
                else
                {
                    return default;
                }
            };
            return currentFilter;
        }
        /// <summary>
        /// 设置之前运行
        /// </summary>
        /// <typeparam name="TFilteer"></typeparam>
        /// <typeparam name="TCurrent"></typeparam>
        /// <param name="currentFilter"></param>
        /// <param name="callback"></param>
        /// <returns></returns>
        public static TCurrent SetBefore<TFilteer, TCurrent>(this TCurrent currentFilter, Func<TCurrent, FilterBase[], Mat, TFilteer> callback) where TCurrent : FilterBase where TFilteer : FilterBase
        {
            currentFilter.BeforeExecute = (a, b, c) =>
            {
                if (b is TCurrent current)
                {
                    return callback?.Invoke(currentFilter, a, c);
                }
                else
                {
                    return default;
                }
            };
            return currentFilter;
        }
    }
}
